"use strict";
const common_vendor = require("../common/vendor.js");
function getCurrentPath() {
  const pages = getCurrentPages();
  const currentPage = pages[pages.length - 1];
  return currentPage.route || "";
}
function getTodayDateKey() {
  const now = /* @__PURE__ */ new Date();
  const year = now.getFullYear();
  const month = String(now.getMonth() + 1).padStart(2, "0");
  const day = String(now.getDate()).padStart(2, "0");
  return `${year}${month}${day}`;
}
function getUserUsageCount() {
  const dateKey = getTodayDateKey();
  const usageKey = `user_usage_${dateKey}`;
  const usageCount = common_vendor.index.getStorageSync(usageKey);
  return usageCount ? parseInt(usageCount) : 0;
}
function increaseUserUsageCount() {
  const dateKey = getTodayDateKey();
  const usageKey = `user_usage_${dateKey}`;
  const currentCount = getUserUsageCount();
  common_vendor.index.setStorageSync(usageKey, (currentCount + 1).toString());
}
function checkUsageLimit() {
  {
    return false;
  }
}
async function callDoubanApi(messages) {
  try {
    // console.info("开始调用API");
    const requestData = {
      // model: "doubao-1.5-vision-pro-250328",
      // model: "doubao-1.5-vision-lite-250315",
      // model: "doubao-seed-1-6-251015",
      model: "doubao-seed-1-6-flash-250828",
      messages,
      thinking: {
        type: "disabled"
      }
    };
    // console.info("data", requestData);
    const response = await common_vendor.index.request({
      url: "https://ark.cn-beijing.volces.com/api/v3/chat/completions",
      method: "POST",
      header: {
        "Content-Type": "application/json",
        "Authorization": `Bearer ${"fc0c505f-8552-418b-ab4e-664018072954"}`
      },
      data: requestData
    });
    if (response.statusCode === 200) {
      return response.data;
    } else {
      throw new Error(`API请求失败: 状态码 ${response.statusCode}, 错误信息: ${response.errMsg || "未知错误"}`);
    }
  } catch (error) {
    console.error("API调用失败:", error);
    common_vendor.index.showToast({
      title: "分析失败，请重试",
      icon: "error",
      duration: 3e3
    });
    throw error;
  } finally {
    common_vendor.index.hideLoading();
  }
}
// 生成图片缓存键
export function generateImageCacheKey(imageBase64) {
  // 取base64字符串的前200个字符和后200个字符来生成唯一标识
  // 这样可以避免缓存键过长
  const prefix = imageBase64.substring(0, 200);
  const suffix = imageBase64.substring(imageBase64.length - 200);
  return `beauty_test_cache_${prefix}_${suffix}`;
}

// 检查并获取缓存的分析结果
export function getCachedAnalysisResult(imageBase64) {
  try {
    const cacheKey = generateImageCacheKey(imageBase64);
    const cachedResult = common_vendor.index.getStorageSync(cacheKey);
    return cachedResult ? JSON.parse(cachedResult) : null;
  } catch (error) {
    console.error('获取缓存结果失败:', error);
    return null;
  }
}

// 缓存分析结果
export function cacheAnalysisResult(imageBase64, analysisResult) {
  try {
    const cacheKey = generateImageCacheKey(imageBase64);
    // 设置缓存，有效期为7天
    common_vendor.index.setStorageSync(cacheKey, JSON.stringify(analysisResult));
    return true;
  } catch (error) {
    console.error('缓存结果失败:', error);
    return false;
  }
}

exports.callDoubanApi = callDoubanApi;
exports.checkUsageLimit = checkUsageLimit;
exports.getCurrentPath = getCurrentPath;
exports.increaseUserUsageCount = increaseUserUsageCount;
exports.generateImageCacheKey = generateImageCacheKey;
exports.getCachedAnalysisResult = getCachedAnalysisResult;
exports.cacheAnalysisResult = cacheAnalysisResult;
